<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>${pageTitle}</title>
    <style>
        body {
            margin: 0;
            font-family: "微软雅黑", sans-serif;
            padding-top: 60px; /* 为固定导航栏留出空间 */
            overflow-x: hidden; /* 防止侧边栏收缩时出现水平滚动条 */
        }

        /* --- 侧边栏样式 (按钮在顶部) --- */
        .sidebar {
            width: 220px;
            height: 100vh;
            background: linear-gradient(135deg, #3a3f44 0%, #2c3136 100%);
            color: #ced4da;
            position: fixed;
            top: 0;
            left: 0;
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            z-index: 100;
            transition: all 0.3s ease-in-out;
            overflow-y: auto;
            display: flex; /* 使用flex垂直布局 */
            flex-direction: column; /* 子元素垂直排列 */
        }

        /* 侧边栏头部 (包含按钮) */
        .sidebar-header {
            padding: 15px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            background-color: #2c3136; /* 稍微深一点的背景 */
        }

        /* 收缩/展开按钮 */
        .toggle-btn {
            background: none;
            border: none;
            color: #e9ecef;
            font-size: 24px;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .toggle-btn:hover {
            transform: scale(1.1);
            color: #ffffff;
        }

        /* 侧边栏菜单区域 */
        .sidebar-menu {
            padding-top: 20px;
            flex-grow: 1; /* 让菜单区域占据剩余空间 */
        }

        /* 侧边栏收缩状态 */
        .sidebar.collapsed {
            width: 60px;
        }

        /* 收缩后隐藏菜单文字 */
        .sidebar.collapsed .sidebar-menu span:not(.icon) {
            display: none;
        }

        /* 收缩后菜单居中 */
        .sidebar.collapsed .sidebar-menu li a {
            justify-content: center;
            padding: 14px 0;
        }

        .sidebar.collapsed .sidebar-menu li a .icon {
            margin-right: 0;
            font-size: 22px;
        }

        /* 收缩后移除hover时的内边距变化 */
        .sidebar.collapsed .sidebar-menu li a:hover {
            padding-left: 0;
        }

        /* 收缩后隐藏侧边栏头部的额外空间 (如果需要) */
        /* .sidebar.collapsed .sidebar-header {
            padding: 15px 0;
        } */

        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar li a {
            display: flex;
            align-items: center;
            padding: 14px 25px;
            color: inherit;
            text-decoration: none;
            transition: all 0.3s ease;
            border-left: 3px solid transparent;
            white-space: nowrap;
        }

        .sidebar li a .icon {
            margin-right: 12px;
            font-size: 20px;
            width: 24px;
            text-align: center;
            transition: transform 0.3s ease;
        }

        .sidebar li a:hover {
            background-color: rgba(255, 255, 255, 0.05);
            color: #ffffff;
            border-left-color: #4dabf7;
            padding-left: 30px;
        }

        .sidebar li a:hover .icon {
            transform: scale(1.2);
        }

        .sidebar li.active a {
            background-color: rgba(77, 171, 247, 0.15);
            color: #4dabf7;
            border-left-color: #4dabf7;
            font-weight: 500;
        }

        .sidebar li.active a .icon {
            color: #4dabf7;
        }

        /* --- 顶部导航栏美化 --- */
        .navbar {
            padding: 15px 25px;
            background: linear-gradient(90deg, #5b646b 0%, #3a3f44 100%);
            color: #e9ecef;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 101;
            margin-left: 220px; /* 与侧边栏宽度保持一致 */
            transition: margin-left 0.3s ease-in-out;
            height: 60px;
            box-sizing: border-box;
        }

        /* 侧边栏收缩时调整导航栏间距 */
        .sidebar.collapsed ~ .navbar {
            margin-left: 60px;
        }

        .navbar-title {
            font-size: 18px;
            font-weight: 500;
            letter-spacing: 0.5px;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info span {
            font-size: 14px;
        }

        .logout-link {
            color: #ffc107;
            text-decoration: none;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            padding: 5px 10px;
            border-radius: 4px;
        }

        .logout-link:hover {
            color: #ffffff;
            background-color: rgba(255, 193, 7, 0.2);
            text-decoration: none;
        }

        /* --- 内容区域调整 --- */
        .content {
            margin-left: 220px; /* 与侧边栏宽度保持一致 */
            padding: 30px 25px;
            transition: margin-left 0.3s ease-in-out;
            min-height: calc(100vh - 60px);
            box-sizing: border-box;
        }

        /* 侧边栏收缩时调整内容区间距 */
        .sidebar.collapsed ~ .content {
            margin-left: 60px;
        }

        /* --- 其他原样式保持不变 --- */
        .search-bar input, .search-bar select {
            padding: 5px;
            margin-right: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table th, table td {
            border: 1px solid #eee;
            padding: 10px;
            text-align: center;
        }

        table th {
            background: #f5f7fa;
        }

        .modal-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        .confirm-dialog {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 350px;
            background: white;
            border-radius: 8px;
            padding: 20px;
            z-index: 1001;
            display: none;
            text-align: center;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
        }

        .confirm-dialog p {
            margin: 0 0 20px 0;
            font-size: 16px;
        }

        .btn {
            padding: 8px 16px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.2s ease;
        }

        .btn-danger {
            background-color: #dc3545;
            color: white;
            border-color: #dc3545;
        }

        .btn-danger:hover {
            background-color: #c82333;
        }

        .btn-secondary {
            background-color: #6c757d;
            color: white;
            border-color: #6c757d;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
        }
    </style>
</head>
<body>
<!-- 侧边导航栏 (按钮在顶部) -->
<div class="sidebar" id="sidebar">
    <!-- 侧边栏头部 -->
    <div class="sidebar-header">
        <button class="toggle-btn" id="toggleBtn">☰</button>
    </div>
    <!-- 侧边栏菜单 -->
    <div class="sidebar-menu">
        <ul>
            <li <c:if test="${pageName == '首页'}">class="active"</c:if>>
                <a href="home?action=page">
                    <span class="icon">🏯</span>
                    <span>系统首页</span>
                </a>
            </li>
            <li <c:if test="${pageName == '用户管理'}">class="active"</c:if>>
                <a href="user?action=list">
                    <span class="icon">👨‍💻</span>
                    <span>用户管理</span>
                </a>
            </li>
            <li <c:if test="${pageName == '图书管理'}">class="active"</c:if>>
                <a href="book?action=list">
                    <span class="icon">💼</span>
                    <span>图书管理</span>
                </a>
            </li>
            <li <c:if test="${pageName == '借阅管理'}">class="active"</c:if>>
                <a href="borrow?action=list">
                    <span class="icon">🚠</span>
                    <span>借阅管理</span>
                </a>
            </li>
            <li <c:if test="${pageName == '归还管理'}">class="active"</c:if>>
                <a href="back_book?action=list">
                    <span class="icon">🔭</span>
                    <span>归还管理</span>
                </a>
            </li>
        </ul>
    </div>
</div>

<!-- 顶部导航栏 -->
<div class="navbar">
    <div class="navbar-title">图书馆管理系统</div>
    <div class="user-info">
        <span>欢迎您，${user.username}</span>
        <span class="logout-link" onclick="showLogoutConfirm()">退出登录</span>
    </div>
</div>

<!-- 登出确认弹窗 -->
<div class="modal-overlay" id="logoutOverlay"></div>
<div class="confirm-dialog" id="logoutConfirm">
    <p>确定要退出登录吗？</p>
    <button class="btn btn-danger" onclick="confirmLogout()">确认退出</button>
    <button class="btn btn-secondary" onclick="cancelLogout()">取消</button>
</div>

<!-- 内容区域 -->
<div class="content">
    <jsp:include page="${contentPage}"/>
</div>

<script>
    // 侧边栏收缩/展开逻辑
    const toggleBtn = document.getElementById('toggleBtn');
    const sidebar = document.getElementById('sidebar');

    toggleBtn.addEventListener('click', () => {
        sidebar.classList.toggle('collapsed');
        // 你可以在这里改变按钮图标，例如从 ☰ 变为 →
        // toggleBtn.textContent = sidebar.classList.contains('collapsed') ? '→' : '☰';
    });

    // 登出确认弹窗控制
    function showLogoutConfirm() {
        document.getElementById("logoutOverlay").style.display = "block";
        document.getElementById("logoutConfirm").style.display = "block";
    }

    function confirmLogout() {
        window.location.href = "${pageContext.request.contextPath}/logout";
    }

    function cancelLogout() {
        document.getElementById("logoutOverlay").style.display = "none";
        document.getElementById("logoutConfirm").style.display = "none";
    }

    // 点击遮罩层关闭弹窗
    document.getElementById("logoutOverlay").addEventListener("click", cancelLogout);

    // 按ESC键关闭弹窗
    document.addEventListener("keydown", function (e) {
        if (e.key === "Escape") {
            cancelLogout();
        }
    });
</script>
</body>
</html>
